package com.small.account.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.small.account.entity.AuPermission;
import com.small.account.mapper.AuPermissionMapper;
import com.small.account.service.IAuPermissionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 服务实现类
 *
 * @author wangguochao
 * @since 2020-01-06
 */
@Service
public class AuPermissionServiceImpl extends ServiceImpl<AuPermissionMapper, AuPermission> implements IAuPermissionService {
    @Resource
    private AuPermissionMapper auPermissionMapper;


    @Override
    public List<AuPermission> findTree(String userName) {
        List<AuPermission> list = auPermissionMapper.findTreeByUserName(userName);
        return recursion(list, null);
    }

    @Override
    public List< AuPermission> findTreeByUserName(String userName,  String scope) {
        List<AuPermission> list = auPermissionMapper.findTreeByUserName(userName);
        List<AuPermission> arr = new ArrayList<>();
        for (AuPermission auPermission : list) {
            if (scope.equals(auPermission.getPermissionCode())){
                List<AuPermission> recursion = this.recursion(list, auPermission.getId());
                auPermission.setChildren(recursion);
                arr.add(auPermission);
            }
        }
        return arr;
    }



    @Override
    public List<AuPermission> findByUserName(String userName) {
        return auPermissionMapper.findTreeByUserName(userName);
    }


    private List<AuPermission> recursion(List<AuPermission> list, String parentId) {
        List<AuPermission> arr = new ArrayList<>();
        for (AuPermission auPermission : list) {
            if (parentId == null && auPermission.getParentId() == null) {
                List<AuPermission> r = recursion(list, auPermission.getId());
                auPermission.setChildren(r);
                arr.add(auPermission);
            } else if (parentId != null && parentId.equals(auPermission.getParentId())) {
                List<AuPermission> r = recursion(list, auPermission.getId());
                auPermission.setChildren(r);
                arr.add(auPermission);
            }

        }
        return arr;
    }

    private List<AuPermission> recursion(List<AuPermission> list, String parentId, String type) {
        List<AuPermission> arr = new ArrayList<>();
        for (AuPermission auPermission : list) {
            if (type.equals(auPermission.getPermissionGroups())) {
                if (parentId == null && auPermission.getParentId() == null) {
                    List<AuPermission> r = recursion(list, auPermission.getId(), type);
                    auPermission.setChildren(r);
                    arr.add(auPermission);
                } else if (parentId != null && parentId.equals(auPermission.getParentId())) {
                    List<AuPermission> r = recursion(list, auPermission.getId(), type);
                    auPermission.setChildren(r);
                    arr.add(auPermission);
                }
            }
        }
        return arr;
    }

    private List<AuPermission> recursion(List<AuPermission> list, String parentId, String type, String type2) {
        List<AuPermission> arr = new ArrayList<>();
        for (AuPermission auPermission : list) {
            if (type.equals(auPermission.getPermissionGroups()) || type2.equals(auPermission.getPermissionGroups())) {
                if (parentId == null && auPermission.getParentId() == null) {
                    List<AuPermission> r = recursion(list, auPermission.getId(), type, type2);
                    auPermission.setChildren(r);
                    arr.add(auPermission);
                } else if (parentId != null && parentId.equals(auPermission.getParentId())) {
                    List<AuPermission> r = recursion(list, auPermission.getId(), type, type2);
                    auPermission.setChildren(r);
                    arr.add(auPermission);
                }
            }
        }
        return arr;
    }


}
